package com.web.global;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.web.manage.bean.Admin;

public class ManageRequestFilter extends HttpServlet implements Filter {
	
	private static final long serialVersionUID = -7189325110831056817L;
	
	public void doFilter(ServletRequest req, ServletResponse res,
			FilterChain chain) throws IOException, ServletException {
		
		req.setCharacterEncoding("GB2312");
		
		HttpServletRequest request = (HttpServletRequest)req;
		HttpServletResponse response = (HttpServletResponse)res;

		String curPage = request.getRequestURI();
		if(curPage.indexOf("/login") == -1 && curPage.indexOf("/css/") == -1 && curPage.indexOf("/images/") == -1 && curPage.indexOf("/js/") == -1) {
			System.out.println(curPage);
			Admin admin = (Admin)request.getSession().getAttribute("admin");
			if(admin == null) {
				response.sendRedirect(request.getContextPath() + "/manage/login.jsp");
				return;
			}else {
				if(!admin.getRoleGroupId().trim().equals("0")) {
					if(!RequestVerify.verifyManage(curPage, admin.getAccount())) {
						response.sendRedirect(request.getContextPath() + "/public/noPower.htm");
						return;
					}
				}
			}
		}
		chain.doFilter(req, res);
	}

	public void init(FilterConfig filterConfig) throws ServletException {
		super.init();
	}
	
	public void destroy() {
		super.destroy();
	}
}